package com.studerb.view.headers;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;

import com.studerb.model.NewsGroup;
import com.studerb.view.MainPanel;

public class HeadersTabbedPane extends JTabbedPane implements ApplicationContextAware {
	private static Logger logger = Logger.getLogger(HeadersTabbedPane.class);
	private final static String CLOSE_COMMAND = "Close Tab";
	private AbstractApplicationContext appContext;
	@Autowired private MainPanel mainPanel;

	public void addNewHeadersTab(NewsGroup newsGroup) {
		// return if we've already got a tab for this group
		if (indexOfTab(newsGroup.getGroupName()) != -1) {
			return;
		}
		HeadersPane headersPane = (HeadersPane) appContext.getBean("headersPane", new Object[] { newsGroup });
		this.addTab(newsGroup.getGroupName(), headersPane);
		mainPanel.getTabbedPane().setSelectedComponent(this);
		setSelectedComponent(headersPane);
	}

	public void init() {
		// AnnotationProcessor.process(this);
		addMouseListener(new HeadersPaneMouseListener());
	}

	/*
	 * @EventSubscriber (eventServiceName = EVENT_SERVICE_NAME) public void
	 * onEvent(NewsGroupEvent evt) { if(evt.getType() ==
	 * NewsGroupEvent.EventType.HEADERS){ NewsGroup newsGroup =
	 * evt.getNewsGroup(); //Add a new GroupTab is we haven't already added this
	 * group, for(SingleHeadersPane shPane : this.newsGroupPanes){
	 * if(shPane.getNewsGroup().equals(newsGroup)){ return; } }
	 * addNewTab(newsGroup); } }
	 */

	@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.appContext = (AbstractApplicationContext) applicationContext;
	}

	class CloseTabActionListener extends AbstractAction {
		private final int tabIndex;

		public CloseTabActionListener(int tabIndex) {
			super();
			this.tabIndex = tabIndex;
		}

		@Override public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(CLOSE_COMMAND)) {
				logger.debug("Closing Tab");
				remove(tabIndex);
			}
		}
	}

	// The mouse listener
	class HeadersPaneMouseListener extends MouseAdapter {

		@Override public void mouseClicked(MouseEvent e) {
			if (SwingUtilities.isRightMouseButton(e)) {
				HeadersTabbedPane headersPane = (HeadersTabbedPane) e.getSource();
				int tabIndex = headersPane.indexAtLocation(e.getX(), e.getY());
				if (tabIndex >= 0) {
					JPopupMenu closePopupMenu = new JPopupMenu();
					JMenuItem menuItem = new JMenuItem(CLOSE_COMMAND);
					closePopupMenu.add(menuItem);
					menuItem.addActionListener(new CloseTabActionListener(tabIndex));
					closePopupMenu.show(headersPane, e.getX(), e.getY() - 10);
				}
			}
		}
	}
}
